<?php

namespace App\Services\Home;

use App\Models\Handroom;

class HandroomService
{
    protected $propertyService;
    protected $regionService;

    public function __construct(PropertyService $propertyService, RegionService $regionService)
    {
        $this->propertyService = $propertyService;
        $this->regionService = $regionService;
    }

    public function getDetailById($id)
    {
        return Handroom::with(['community'])->findOrFail($id);
    }

    public function data($request)
    {
//        $model = Handroom::query();
//        if($request->input('keyword')){
//            $model = $model->where('title','like','%'.$request->input('keyword').'%');
//        }
//        if($request->input('community_id')){
//            $model = $model->where('community_id',$request->input('community_id'));
//        }
//        if ($request->input('town')){
//            $model = $model->whereIn('town_id',$request->input('town'));
//        }else{
//            if ($request->input('area') && $request->input('area') != 0){
//                $ids = $this->regionService->getIds($request->input('area'));
//                $model = $model->whereIn('area',$ids);
//            }
//            if ($request->input('station')){
//                $model = $model->whereIn('metro_station_id',$request->input('station'));
//            }else{
//                if($request->input('metro_id') > 0){
//                    $model = $model->where('metro_id',$request->input('metro_id'));
//                }
//            }
//        }
//
//        if ($request->input('min_price')>=0 && $request->input('max_price')>0){
//            $model = $model->whereBetween('price',[$request->input('min_price'),$request->input('max_price')]);
//        }
//        if ($request->input('min_acreage')>=0 && $request->input('max_acreage')>0){
//            $model = $model->whereBetween('acreage',[$request->input('min_acreage'),$request->input('max_acreage')]);
//        }
//        if (count($request->input('apartment'))){
//            if (in_array(5,$request->input('apartment'))){
//                $apartment = $request->input('apartment');
//                $model = $model->where(function ($query) use ($apartment) {
//                    $query->whereIn('room', $apartment)
//                        ->orWhere('room', '>', 5);
//                });
//            }else{
//                $model = $model->whereIn('room',$request->input('apartment'));
//            }
//        }
//        if ($request->input('tags')){
//            $ids = $this->propertyService->getPropertyByAttrId($request->input('tags'),'hand');
//            $model = $model->whereIn('id',$ids);
//        }
//        if ($request->input('floor')){
//            $model = $model->whereIn('floor',$request->input('floor'));
//        }
//        if ($request->input('direction')){
//            $model = $model->whereIn('category_id',$request->input('direction'));
//        }
//        if ($request->input('renovation')){
//            $model = $model->whereIn('renovation',$request->input('renovation'));
//        }
//        if ($request['year']) {
//            $age = explode(',', $request->input('year'));
//            $min = date('Y') - $age[1];
//            $max = date('Y') - $age[0];
//            $model = $model->whereBetween('year', [$min, $max]);
//        }
//        if($request->input('sort') == 'default'){
//            $model = $model->orderBy('created_at','desc');
//        }elseif($request->input('sort') == 'asc'){
//            $model = $model->orderBy('price','asc');
//        }elseif($request->input('sort') == 'desc'){
//            $model = $model->orderBy('price','desc');
//        }elseif($request->input('sort') == 'popular'){
//            $model = $model->orderBy('follow','desc');
//        }
        $res = Handroom::with('attribute')
            ->valid($request->input('sort'))
            ->filter($request->all())
            ->paginateFilter($request->input('pagesize',10));

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'total' => $res['total'],
            'data'  => $res
        ];
        return response()->json($data);
    }
}